package com.eric.downloader;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class AlibabaRecDownloader extends SimplePageDownloader {
	
	String queryBaseUrl = "https://s.1688.com/selloffer/offer_search.htm?keywords=";
	
	String kw;
	
	public static void main(String[] args) {
		AlibabaRecDownloader bdr = new AlibabaRecDownloader("美妆");
		
		System.out.println(bdr.analyze());
	}
	
	public AlibabaRecDownloader (String kw) {
		this.kw = kw;
	}
	
	public JSONObject analyze(String kw) {
		this.kw = kw;
		return analyze();
	}


	public JSONObject analyze() {
		String s = null;
		try {
			s = URLEncoder.encode(kw,"gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		
		querySearchEngine(queryBaseUrl,s);
		
		JSONObject json = new JSONObject();
		List<String> list = new ArrayList<>();
		String[] a = doc.getElementsByAttributeValue("t-rule-id", "960").select("a").text().split(" ");
		for (int i=0;i<a.length;i++) {
			list.add(a[i]);
		}
		json.put("kw", kw);
		json.put("rec", list);
		return json;
	}

}
